package com.beiyou.controller;

import com.beiyou.mapper.ActivityMapper;
import com.beiyou.model.Activity;
import com.beiyou.model.User;
import com.beiyou.service.impl.ActivityServiceImpl;
import com.beiyou.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/activityServlet")
public class ActivityServlet  extends HttpServlet {
    private ActivityServiceImpl aservice = new ActivityServiceImpl();

    // 获取活动列表页面的信息，进行查询活动操作
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 防止乱码
        req.setCharacterEncoding("utf-8");

        String opsCode = req.getParameter("opsCode");
        String activity_id = req.getParameter("activity_id");

        // 点击删除活动
        if("delActivity".equals(opsCode)){
            int result = aservice.delActivity(activity_id);
            if(result>0){
                // 刷新活动列表界面
                List<Activity> activityList = aservice.getAllActivity();
                req.setAttribute("activityList",activityList);
                req.getRequestDispatcher("UTotalActivityList.jsp").forward(req,resp);
                System.out.println("删除活动成功");
            }
            else{
                System.out.println("删除活动失败");
            }
        }
        // 点击修改
        if("modifyActivity".equals(opsCode)){
            // 将该活动id传入会话，在提交表单时使用
            HttpSession session = req.getSession();
            session.setAttribute("activity_id",activity_id);

            req.getRequestDispatcher("modifyActivity.jsp").forward(req,resp);
        }
    }

    // 获取活动列表页面的信息，进行删除活动操作
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 防止乱码
        req.setCharacterEncoding("utf-8");

        String operate = req.getParameter("operate");

        // 获取前端输入的活动信息
        String activity_name = req.getParameter("activity_name");
        String location = req.getParameter("location");
        String activity_time = req.getParameter("activity_time");
        String activity_description = req.getParameter("activity_description");

        Activity activity = new Activity();
        activity.setActivity_name(activity_name);
        activity.setLocation(location);
        activity.setActivity_time(activity_time);
        activity.setActivity_description(activity_description);

        // 提交修改活动的表单
        if(operate==null) {
            java.util.Map<String,Object> activityMap = new HashMap<>();
            activityMap.put("activity_name",activity_name);
            activityMap.put("location",location);
            activityMap.put("activity_time",activity_time);
            activityMap.put("activity_description",activity_description);
            // 通过点击修改按钮时提交的会话信息获取该活动的id
            HttpSession session = req.getSession(false); // 不创建新会话，过去前端会话信息
            String id = (String) session.getAttribute("activity_id");
            activityMap.put("id",id);
            int result= aservice.updateActivity(activityMap);
            if(result>0){
                System.out.println("修改成功！---");
                // 修改完毕，跳转到活动列表界面
                // 刷新活动列表界面
                List<Activity> activityList = aservice.getAllActivity();
                req.setAttribute("activityList",activityList);
                req.getRequestDispatcher("UTotalActivityList.jsp").forward(req,resp);
            }
            return;
        }

        // 点击查询
        if(operate.equals("search")) {
            // 模糊查询操作
            List<Activity> activityList = aservice.getActivityByCon(activity);
            // 刷新活动列表页面
            req.setAttribute("activityList",activityList);
            req.getRequestDispatcher("UTotalActivityList.jsp").forward(req,resp);
            System.out.println("查询活动成功");
            return;
        }

        // 提交添加活动表单
        if(operate.equals("add")){
            // 添加操作
            if(activity.getManager_id()==null){
                activity.setManager_id("111");
            }
            int result = aservice.addActivity(activity);
            if(result>0){
                System.out.println("添加活动成功");
            }
            // 全部查询操作
            List<Activity> activityList = aservice.getAllActivity();
            // 刷新活动列表页面
            req.setAttribute("activityList",activityList);
            req.getRequestDispatcher("UTotalActivityList.jsp").forward(req,resp);
            return;
        }
    }
}
